// express 基于Node.js平台，快速、开放、极简的 Web 开发框架 https://www.expressjs.com.cn/
const express = require("express");
const app = express();
const cors = require("cors");
const bodyParser = require("body-parser");
const jsonParser = bodyParser.json();
const fs = require("fs");

// 配置cors中间件，允许跨域
app.use(cors());
app.use(express.urlencoded());

app.get("/getVue2Str", (req, res) => {
    // 服务端读取文件，并变成字符串。传递给前端
    const data = fs.readFileSync("./files/vue/loaderVue2.vue", "utf8");

    res.send({
        code: 200,
        fileStr: data,
        fileName: "loaderVue2.vue",
    });
});

app.post("/setVue3Str", jsonParser, (req, res) => {
    // 服务端读取文件，并变成字符串。传递给前端
    console.log(req.body.fileName)
    // console.log(res.json(req.body))

    fs.writeFileSync("./files/vue/loaderVue3.vue", req.body.fileStr);

    res.send({
        code: 200,
        msg: "保存成功",
    });
});

app.get("/getVue3Str", (req, res) => {
    // 服务端读取文件，并变成字符串。传递给前端
    const data = fs.readFileSync("./files/vue/loaderVue3.vue", "utf8");

    res.send({
        code: 200,
        fileStr: data,
        fileName: "loaderVue2.vue",
    });
});

app.listen(
    3000,
    () => console.log("服务启动成功：http://localhost:3000")
);
